package com.htgd.gardenexpo.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dao.GeBaseDictDao;
import com.htgd.gardenexpo.dao.GeConIntegralGoodsOrderMapper;
import com.htgd.gardenexpo.dto.GoodsOrderQuery;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConIntegralGoodsOrder;
import com.htgd.gardenexpo.service.GeConIntegralGoodsOrderService;
import com.htgd.gardenexpo.utils.TokenUtil;
import com.htgd.gardenexpo.vo.GoodsExchangeNumsVO;
import com.htgd.gardenexpo.vo.GoodsNumsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Objects;

/**
 * @author: 王秀田
 * @date: 2024/11/14 15:47
 * @description: TODO
 */
@Service
public class GeConIntegralGoodsOrderServiceImpl extends ServiceImpl<GeConIntegralGoodsOrderMapper, GeConIntegralGoodsOrder> implements GeConIntegralGoodsOrderService {

    @Autowired
    private GeConIntegralGoodsOrderMapper geConIntegralGoodsOrderMapper;

    @Autowired
    private GeBaseDictDao geBaseDictDao;

    @Override
    public PageInfo<GeConIntegralGoodsOrder> pageList(GoodsOrderQuery goodsOrderQuery, HttpServletRequest request) {
        PageHelper.startPage(goodsOrderQuery.getPageNum(), goodsOrderQuery.getPageSize());
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        LambdaQueryWrapper<GeConIntegralGoodsOrder> lambdaQueryWrapper = new LambdaQueryWrapper<GeConIntegralGoodsOrder>();
        if(goodsOrderQuery.getValue()!=null&&goodsOrderQuery.getValue()!=""){
            lambdaQueryWrapper
                    .like(GeConIntegralGoodsOrder::getName,goodsOrderQuery.getValue())
                    .or()
                    .like(GeConIntegralGoodsOrder::getPhone,goodsOrderQuery.getValue())
                    .or()
                    .like(GeConIntegralGoodsOrder::getGoodsName,goodsOrderQuery.getValue())
                    .or()
                    .like(GeConIntegralGoodsOrder::getOrderNum,goodsOrderQuery.getValue());
        }
        if(goodsOrderQuery.getStartTime()!=null&&goodsOrderQuery.getStartTime()!=""
                &&goodsOrderQuery.getEndTime()!=null&&goodsOrderQuery.getEndTime()!=""){
            lambdaQueryWrapper
                    .and(w->w.between(GeConIntegralGoodsOrder::getCreateTime,
                            goodsOrderQuery.getStartTime(),goodsOrderQuery.getEndTime()));
        }
        if(Objects.nonNull(openId)&&openId!=""){
            lambdaQueryWrapper.eq(GeConIntegralGoodsOrder::getOpenId,openId);
            // 计算最近两个月的起始和结束日期
            LocalDate now = LocalDate.now();
            // 添加查询条件，createDate在最近两个月内
            lambdaQueryWrapper.ge(GeConIntegralGoodsOrder::getCreateTime, now.minus(2, ChronoUnit.MONTHS));
            lambdaQueryWrapper.orderByDesc(GeConIntegralGoodsOrder::getCreateTime);
        }
        List<GeConIntegralGoodsOrder> list = geConIntegralGoodsOrderMapper.selectList(lambdaQueryWrapper);
        list.forEach(item->{
            item.setImages(JSONArray.parseArray(item.getPicture(),String.class));
        });
        return new PageInfo<>(list);
    }

    @Override
    public CommonResult<GoodsExchangeNumsVO> getGoodsExchangeNums(Integer pageNum, Integer pageSize, String orderBy) {
        GoodsExchangeNumsVO goodsExchangeNumsVO = new GoodsExchangeNumsVO();

        //获取总数
        Long count = lambdaQuery().
                eq(GeConIntegralGoodsOrder::getStatus,"0")
                .count();
        goodsExchangeNumsVO.setTotalNums(count);

        //每类商品数量
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsNumsVO> goodsNumsVOS = geConIntegralGoodsOrderMapper.getGoodsExchangeNums(orderBy);
        for (GoodsNumsVO goodsNumsVO : goodsNumsVOS) {
            String goods_type = geBaseDictDao.getTextByCodeAndValue("goods_type", goodsNumsVO.getType());
            goodsNumsVO.setType(goods_type);
        }
        goodsExchangeNumsVO.setGoodsNumsVOS(goodsNumsVOS);

        return CommonResult.success("查询成功", goodsExchangeNumsVO);
    }
}
